package com.hly.wms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hly.common.constant.WareConstant;
import com.hly.wms.feign.RenRenFeign;
import com.hly.wms.vo.MergeVo;
import com.hly.wms.vo.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hly.wms.entity.PurchaseEntity;
import com.hly.wms.service.PurchaseService;
import com.hly.common.utils.PageUtils;
import com.hly.common.utils.R;



/**
 * 采购信息
 *
 * @author dudulu
 * @email 52039@Miku.com
 * @date 2021-03-31 15:49:06
 */
@RestController
@RequestMapping("wms/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private RenRenFeign renRenFeign;

    /**
     * 完成采购单
     * @param purchaseDoneVo
     * @return
     */
    @RequestMapping("done")
    public R done(@RequestBody PurchaseDoneVo purchaseDoneVo){
        purchaseService.done(purchaseDoneVo);
        return R.ok();
    }

    /**
     * 领取采购单
     * @param items
     * @return
     */
    @RequestMapping("/received")
    public R received(@RequestBody List<Long> ids){
        purchaseService.received(ids);
        return R.ok();
    }

    /**
     * 查询所有用户
     * @param params
     * @param s
     * @return
     */
    @RequestMapping("/getBySysUserAll")
    public R getBySysUserAll(@RequestParam Map<String, Object> params , @RequestHeader("token") String s ){
        System.out.println(s);
        return renRenFeign.list(params);
    }

    /**
     * 批量整合订单
     * @param mergeVo
     * @return
     */
    @RequestMapping("/MergerOrder")
    public R MergerOrder(@RequestBody MergeVo mergeVo){
        if (!purchaseService.MergerOrder(mergeVo)){
            return R.error(-1,"失败！！！那个已分配");
        }
        return R.ok().put("msg","整合成功，嘟嘟噜~");
    }

    @RequestMapping("/getPurchases")
    public R getPurchases(){
        List<PurchaseEntity> list = purchaseService.getPurchases();
        return R.ok().put("data",list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PurchaseEntity purchase){
        purchase.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
        purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PurchaseEntity purchase){
		purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 修改，采购分配任务
     */
    @RequestMapping("/UpdateById")
    public R UpdateById(@RequestParam("assigneeId") Integer assigneeId, @RequestParam("id") Integer id){
        if(purchaseService.updateByIdOfUser(assigneeId,id)){
            return R.ok();
        }
        return R.error(-1,"错误，请稍后重试！");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
